<?php
namespace Api\Controller;
use Think\Controller;
class IndexController extends CommonController {
    public function weather(){
        $map['wid'] = session('wid');
        
        $out = $this->ihttp_get("https://free-api.heweather.com/s6/weather/forecast?key=6baa4e9e59e44436b38407962919148c&location={$_GET['city']}", 0);
        // $this->ajaxReturn($res);
        $td = json_decode($out, 1);
        $data = $td['HeWeather6'][0]['daily_forecast'][0];

        $hour = date("H");
        $weeks = array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
        $w = date('w');
        if($hour < 5 || $hour > 19){
            $t = 'n';
        }else{
            $t = 'd';
        }
        // print_r($data);

        $res = array(
            'city'=>$_GET['city'],
            'wendu'=>"{$data['tmp_min']}-{$data['tmp_max']}°C",
            'tianqi'=>$data['cond_txt_'.$t],
            'code'=>$data['cond_code_'.$t],
            'code_img'=>'https://www.58zltc.com/Public/tianqi/'.$data['cond_code_'.$t].'.png',
            'date'=>date("Y-m-d"),
            'week'=>$weeks[$w],
            'type'=>'duoyun'
        );
        if(in_array($res['tianqi'], array('晴','少云','晴间多云','平静','热'))){
            $type = 'sunny';
        }elseif(preg_match('/风/', $res['tianqi'])){
            $type = 'wind';
        }elseif(preg_match('/雨/', $res['tianqi'])){
            $type = 'rain';
        }elseif(preg_match('/雪/', $res['tianqi'])){
            $type = 'snow';
        }elseif($res['tianqi'] == 'duoyun'){
            $type = 'duoyun';
        }else{
            $type = 'cloudy';
        }
        // $new = array('sunny','wind','rain','snow','duoyun','cloudy');
        // $num = rand(0,5);
        $res['type'] = $type;
        // echo $res;
        $this->ajaxReturn($res);
    }
    //独立小程序获取shopid
    public function getShopid()
    {
        $appid = I("appid");
        $wechat =M("wechat")->where(['appid'=>$appid])->find();
        if($wechat['shop_id']){
            $this->ajaxReturn(['code'=>0,'shop_id'=>$wechat['shop_id']]);
        }else{
            $this->ajaxReturn(['code'=>400,'msg'=>"访问失败"]);
        }
    }

    public function getAddressFilter(){
        $map['wid'] = session('wid');
        $wechat = M("Wechat")->where($map)->find();

        $where['city_code'] = $wechat['city'];
        $city = M("City")->where($where)->find();
        $where1['pid'] = $city['id'];
        $site_arr2 = M("City")->where($where1)->select();
        if($wechat['city'] == 0){
            $site_arr1_id = 0;
            $site_arr1 = "全国";
        }else{
            $site_arr1_id = $city['id'];
            $site_arr1 = $city["city_name"];
            if(!$site_arr2){
                $site_arr2 = array(array('id'=>0,'city_name'=>"全辖区"));
            }
        }

        $arr['title'] = '地区';
        $arr['is_show'] = false;
        $arr['list'] = array();
        $arr['list'][0] = array('id'=>0,'value'=>'全部', 'is_selected'=>true);
        $arr['list'][1] = array('id'=>$site_arr1_id,'value'=>$site_arr1, 'is_selected'=>false);
        foreach ($site_arr2 as $key => $value) {
            $arr['list'][1]['sub_list'][$key] = array('id'=>$value['id'] ,'value'=>$value['city_name'], 'is_selected'=>true);
        }
        return $this->ajaxReturn($arr);
    }

    public function adv (){
        $map['wid'] = session('wid');
        $map['type'] = $_GET['type'];
        $adv = M("adv")->where($map)->order("last_time desc")->getField('imgurl',true);
        $this->ajaxReturn($adv);
    }

    //
    public function getFlash(){
        $map['wid'] = session('wid');
        $map['type'] = 'home';
        $adv = M("adv")->where($map)->order("last_time desc")->getField('imgurl',true);
        $this->ajaxReturn($adv);
    }

    // 首页数据生成方法
    public function index(){
        if (isset($_GET['pageNum'])) {
            $page = I('pageNum');
            $page = ((int)$page + 1) * 10;
        } else {
            $page = 0;
        }

        $map['wid'] = session('wid');
        $map['is_pay'] = 1;
        if($_GET['openid']){
            $map['openid'] = $_GET['openid'];
        }
        //同城头条
        $list = M("InfoOrder")->where($map)->order("is_top desc,top_end_time desc,last_time desc")->limit(0, $page)->select();
        $eq['wid'] = session('wid');
        $eq['status'] = 1;
        $new['list'] = array();
        foreach ($list as $k => $v) {
            $eq['id'] = $v['info_id'];
            switch ($v['action']) {
                case 'job':
                    $vo = M("Job")->where($eq)->order("last_time desc")->find();
                    if(!$vo){
                        break;
                    }
                    if($vo['is_top'] == 1 && $vo['top_end_time'] < date("Y-m-d H:i:s")){
                        $vo['is_top'] = 0;
                    }
                    $vo['action'] = 'job';
                    $vo['tags'] = explode(',', $vo['tag']);
                    $new['list'][] = $vo;
                    break;
                case 'house':
                    $vo = M("House")->where($eq)->order("last_time desc")->find();
                    if(!$vo){
                        break;
                    }
                    if($vo['is_top'] == 1 && $vo['top_end_time'] < date("Y-m-d H:i:s")){
                        $vo['is_top'] = 0;
                    }
                    $vo['action'] = 'house';
                    $vo['tags'] = explode(',', $vo['tag']);
                    $new['list'][] = $vo;
                    break;
                case 'info':
                    $eq1['tp_info.wid'] = session('wid');
                    $eq1['tp_info.status'] = 1;
                    $eq1['tp_info.id'] = $v['info_id'];
                    $vo = M("Info")->join('tp_member ON tp_info.openid = tp_member.openid')->where($eq1)->field("tp_info.*, tp_member.nickname, tp_member.headimgurl")->order("last_time desc")->find();
                    if(!$vo){
                        break;
                    }
                    if($vo['is_top'] == 1 && $vo['top_end_time'] < date("Y-m-d H:i:s")){
                        $vo['is_top'] = 0;
                    }
                    $vo['action'] = 'info';
                    $vo['tags'] = explode(',', $vo['tag']);
                    $new['list'][] = $vo;
                    break;
                case 'pinche':
                    // $vo = M("Pinche")->where($eq)->order("last_time desc")->find();
                    // $vo['tags'] = explode(',', $vo['tag']);
                    // $new[] = $vo;
                    break;
                
                default:
                    # code...
                    break;
            }
        }
        //排序模板
        $tui = M('tui_cate')->where(['status'=>1])->order("sort desc")->select();
        $arr = [];
            //查出对应模板的信息
        foreach($tui as $k=>$v){
            $tuilist = M('tui')->where(['tid'=>$v['id'],'status'=>1])->select();
            foreach($tuilist as $kk=>$vv){
                //用户
                if($vv['type'] == "member"){
                    $news = M('member')->where(['mid'=>$vv['aid']])->find();
                    $data[$k]['type'] = 'member';
                    $arr[$v['type']][] =$news;
                }
                //推荐文章
                if($vv['type'] == "news"){
                    $news = M('news')->where(['id'=>$vv['aid']])->find();

                    $news['pics'] = explode(',', $news['pics']);
                    $count = M('news_reply')
                        ->where(array('nid'=>$news['id']))
                        ->group('openid')
                        ->count('id');
                    if(empty($count)){
                        $count = 0;
                    }
                    $news['reply_num'] = $count;
                    $news['add_time'] = $this->getAnnounceTime($value['create_time']);

                    $data[$k]['type'] = 'news';
                    $arr[$v['type']][] =$news;
                }

                //按顺序返回
                $data[$k][$v['type']] = $arr[$v['type']];
                unset($news);

            }
        }
        $new['arr'] = $data;
        $this->ajaxReturn($new);
    }

    public function mine(){
        $map['openid'] = $_POST['openid'];
        $map['wid'] = session('wid');
        $map['is_pay'] = 1;
        $where['wid'] = session('wid');
        $where['openid'] = I('openid');
        //发布
        $info['info_num'] = M("InfoOrder")->where($map)->count();
        //余额，商家加余额
        $shop = M("Shop")->where($map)->find();
        if ($shop) {
            $info['balance_money'] = $shop['money'];
        }else{
            $info['balance_money'] = 0;
        }
        //优惠券数量
        $info['coupon_num'] = M("CouponLog")->where($where)->order("create_time desc")->count();
        //关注
        $ShopFollow = M('ShopFollow') ->where(array('openid'=>$_POST['openid']))->count();
        $info['ShopFollow'] = $ShopFollow;
        $this->ajaxReturn($info);

    }

    public function getCitySelect(){
        $map['wid'] = session('wid');
        $wechat = M("Wechat")->where($map)->find();

        $where['city_code'] = $wechat['city'];
        $city = M("City")->where($where)->find();
        $where1['pid'] = $city['id'];

        $site_arr = array(
            'title'=>'全部地区',
            'is_show'=>false,
            'list'=>array(

            )
        );

        $site_arr2 = M("City")->where($where1)->getField("city_name",true);
        if($wechat['city'] == 0){
            $site1 = "全国";
        }else{
            $site1  = $city["city_name"];
            if(!$site_arr2){
                $site_arr2 = array("全辖区");
            }
        }

        foreach ($site_arr2 as $key => $value) {
            $site_arr2[$key] = array(
                'value'=> $value,
                'is_selected'=>false
            );
        }
        $site_arr = array(
            'title'=>'全部地区',
            'is_show'=>false,
            'list'=>array(
                'value'=> $site1,
                'is_selected'=>false
            )
        );
        if($site_arr2){
            $site_arr['list']['sub_list'] = $site_arr2;
        }
        $this->ajaxReturn($site_arr);
    }

    public function getAddInfo(){
        $map['wid'] = session('wid');
        $wechat = M("Wechat")->where($map)->find();

        $map['id'] = $_GET['id'];

        $info = M("Cate")->where($map)->find();
        $form = array();
        $tags = array();
        $res = array();
        if(!empty($info['tags'])){
            $info['tags'] = explode(',', $info['tags']);
            foreach ($info['tags'] as $key => $value) {
                $tags[] = array('title'=>$value, 'selected'=>false);
            }
        }else{
            $tags = array();
        }
        $tags_input = array('type'=>'tag', 'title'=>'标签选择', 'name'=>'tag', 'tags'=>$tags, 'is_show_tags_input'=>false, 'tags_input_value'=>'');
        
        $top_input = array('type'=>'top');

        $where['city_code'] = $wechat['city'];
        $city = M("City")->where($where)->find();
        $where1['pid'] = $city['id'];
        $site_arr2 = M("City")->where($where1)->getField("city_name",true);
        if($wechat['city'] == 0){
            $site_arr1 = array("全国");
        }else{
            $site_arr1 = array($city["city_name"]);
            if(!$site_arr2){
                $site_arr2 = array("全辖区");
            }
        }
        $site_input = array('type'=>'picker2', 'title'=>'区域', 'name'=>array('county_name', 'site_name') 
                ,'range'=>array($site_arr1, $site_arr2)
                , 'value'=>array(0,0));
        // $form[] = array('type'=>'pic', 'title'=>'房源图片', 'name'=>'pics', 'num'=>1);

        // print_r($info);
        $res['info'] = $info;
        if($info['sub_type'] == 'quanzhi'){
            $res['add_url'] = "https://www.58zltc.com/api.php/Job/add?type=1";
            //$res['add_url'] = "https://www.58zltc.com/api.php/Job/add?type=1";
            $res['index_url'] = "/pages/index/job/index";

            $form[] = array('type'=>'text', 'title'=>'职位名称', 'name'=>'title');
            $form[] = array('type'=>'picker1', 'title'=>'薪资水平', 'name'=>'pay_money' 
                ,'range'=>array('面议', '1000元以下', '1000-2000元', '2000-3000元', '3000-5000元', '5000-8000元', '8000-12000元', '12000-20000元', '20000-25000元', '25000元以上')
                , 'value'=>0);
            $form[] = $site_input;
            $form[] = array('type'=>'picker1', 'title'=>'工作年限', 'name'=>'job_year' 
                ,'range'=>array('不限', '1年以下', '1-2年', '3-5年', '6-7年', '8-10年', '10年以上')
                , 'value'=>0);
            $form[] = array('type'=>'picker1', 'title'=>'学历要求', 'name'=>'education' 
                ,'range'=>array('不限', '小学', '初中', '高中', '大专', '本科', '硕士', '博士')
                , 'value'=>0);
            $form[] = array('type'=>'number', 'title'=>'招聘人数', 'name'=>'num');
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'text', 'title'=>'公司名称', 'name'=>'company_name');
            $form[] = array('type'=>'text', 'title'=>'详细地址', 'name'=>'company_site');
            $form[] = array('type'=>'number', 'title'=>'联系电话', 'name'=>'tel');
            $form[] = array('type'=>'top');
            $form[] = $tags_input;
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'textarea', 'title'=>'职位详情', 'name'=>'content');
        }elseif($info['sub_type'] == 'qiu'){
            $res['add_url'] = "https://www.58zltc.com/api.php/Job/add?type=2";
            //$res['add_url'] = "https://www.58zltc.com/api.php/Job/add?type=2";
            $res['index_url'] = "/pages/index/job/index";

            $form[] = array('type'=>'text', 'title'=>'姓名', 'name'=>'username');
            $form[] = array('type'=>'text', 'title'=>'意向岗位', 'name'=>'title');
            $form[] = array('type'=>'picker1', 'title'=>'意向薪资', 'name'=>'pay_money' 
                ,'range'=>array('面议', '1000元以下', '1000-2000元', '2000-3000元', '3000-5000元', '5000-8000元', '8000-12000元', '12000-20000元', '20000-25000元', '25000元以上')
                , 'value'=>0);
            $form[] = $site_input;
            $form[] = array('type'=>'picker1', 'title'=>'工作经验', 'name'=>'job_year' 
                ,'range'=>array('不限', '1年以下', '1-2年', '3-5年', '6-7年', '8-10年', '10年以上')
                , 'value'=>0);
            $form[] = array('type'=>'picker1', 'title'=>'学历', 'name'=>'education' 
                ,'range'=>array('不限', '小学', '初中', '高中', '大专', '本科', '硕士', '博士')
                , 'value'=>0);
            $form[] = array('type'=>'picker1', 'title'=>'性别', 'name'=>'sex' 
                ,'range'=>array('男', '女')
                , 'value'=>0);
            $form[] = array('type'=>'number', 'title'=>'年龄', 'name'=>'age');
            $form[] = array('type'=>'picker1', 'title'=>'求职类型', 'name'=>'qiu_type' 
                ,'range'=>array('全职', '兼职')
                , 'value'=>0);
            $form[] = array('type'=>'number', 'title'=>'联系电话', 'name'=>'tel');
            $form[] = array('type'=>'text', 'title'=>'电子邮箱', 'name'=>'email');
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'textarea', 'title'=>'个人简介', 'name'=>'content');
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'pic', 'title'=>'简历头像', 'name'=>'headimg', 'num'=>1, 'pics'=>array());
            $form[] = array('type'=>'top');
            $form[] = $tags_input;
        }elseif($info['sub_type'] == 'jianzhi'){
            $res['add_url'] = "https://www.58zltc.com/api.php/Job/add?type=3";
            $res['index_url'] = "/pages/index/job/index";

            $form[] = array('type'=>'text', 'title'=>'职位名称', 'name'=>'title');
            $form[] = array('type'=>'picker1', 'title'=>'薪资水平', 'name'=>'pay_money' 
                ,'range'=>array('面议', '1000元以下', '1000-2000元', '2000-3000元', '3000-5000元', '5000-8000元', '8000-12000元', '12000-20000元', '20000-25000元', '25000元以上')
                , 'value'=>0);
            $form[] = $site_input;
            $form[] = array('type'=>'picker1', 'title'=>'工作年限', 'name'=>'job_year' 
                ,'range'=>array('不限', '1年以下', '1-2年', '3-5年', '6-7年', '8-10年', '10年以上')
                , 'value'=>0);
            $form[] = array('type'=>'picker1', 'title'=>'学历要求', 'name'=>'education' 
                ,'range'=>array('不限', '小学', '初中', '高中', '大专', '本科', '硕士', '博士')
                , 'value'=>0);
            $form[] = array('type'=>'number', 'title'=>'招聘人数', 'name'=>'num');
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'text', 'title'=>'公司名称', 'name'=>'company_name');
            $form[] = array('type'=>'text', 'title'=>'详细地址', 'name'=>'company_site');
            $form[] = array('type'=>'number', 'title'=>'联系电话', 'name'=>'tel');
            $form[] = array('type'=>'top');
            $form[] = $tags_input;
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'textarea', 'title'=>'职位详情', 'name'=>'content');
        }elseif($info['sub_type'] == 'new'){
            $res['add_url'] = "https://www.58zltc.com/api.php/House/add?type=1";
            $res['index_url'] = "/pages/index/house/index";

            $form[] = array('type'=>'text', 'title'=>'楼盘名称', 'name'=>'title');
            $form[] = $site_input;
            $form[] = array('type'=>'date', 'title'=>'开盘时间', 'name'=>'house_start_time', 'value'=>date("Y-m-d"));
            $form[] = array('type'=>'text', 'title'=>'面积范围', 'name'=>'house_area_str');
            $form[] = array('type'=>'number', 'title'=>'均价', 'name'=>'price', 'unit'=>'元/㎡');
            $form[] = array('type'=>'number', 'title'=>'物管费', 'name'=>'house_wuguan_fee');
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'text', 'title'=>'开发商', 'name'=>'house_company');
            $form[] = array('type'=>'text', 'title'=>'楼盘地址', 'name'=>'house_address');
            $form[] = array('type'=>'number', 'title'=>'销售电话', 'name'=>'tel');
            $form[] = array('type'=>'top');
            $form[] = $tags_input;
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'textarea', 'title'=>'房源详情', 'name'=>'content');
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'pic', 'title'=>'房源图片', 'name'=>'pics', 'num'=>2, 'pics'=>array());
        }elseif($info['sub_type'] == 'ershou'){
            $res['add_url'] = "https://www.58zltc.com/api.php/House/add?type=2";
            $res['index_url'] = "/pages/index/house/index";

            $form[] = array('type'=>'text', 'title'=>'标题', 'name'=>'title');
            $form[] = $site_input;
            $form[] = array('type'=>'text', 'title'=>'小区名称', 'name'=>'block_name');
            $form[] = array('type'=>'text', 'title'=>'户型', 'name'=>'house_type');
            $form[] = array('type'=>'number', 'title'=>'面积', 'name'=>'house_area');
            $form[] = array('type'=>'number', 'title'=>'总价', 'name'=>'price', 'unit'=>'万元');
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'text', 'title'=>'联系人', 'name'=>'username');
            // $form[] = array('type'=>'text', 'title'=>'详细地址', 'name'=>'company_site');
            $form[] = array('type'=>'number', 'title'=>'联系电话', 'name'=>'tel');
            $form[] = array('type'=>'top');
            $form[] = $tags_input;
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'textarea', 'title'=>'房源详情', 'name'=>'content');
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'pic', 'title'=>'房源图片', 'name'=>'pics', 'num'=>2, 'pics'=>array());
        }elseif($info['sub_type'] == 'zufang'){
            $res['add_url'] = "https://www.58zltc.com/api.php/House/add?type=3";
            $res['index_url'] = "/pages/index/house/index";

            $form[] = array('type'=>'text', 'title'=>'标题', 'name'=>'title');
            $form[] = $site_input;
            $form[] = array('type'=>'text', 'title'=>'小区名称', 'name'=>'block_name');
            $form[] = array('type'=>'text', 'title'=>'户型', 'name'=>'house_type');
            $form[] = array('type'=>'number', 'title'=>'面积', 'name'=>'house_area');
            $form[] = array('type'=>'number', 'title'=>'租金', 'name'=>'price', 'unit'=>'元/月');
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'text', 'title'=>'联系人', 'name'=>'username');
            // $form[] = array('type'=>'text', 'title'=>'详细地址', 'name'=>'company_site');
            $form[] = array('type'=>'number', 'title'=>'联系电话', 'name'=>'tel');
            $form[] = array('type'=>'top');
            $form[] = $tags_input;
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'textarea', 'title'=>'房源详情', 'name'=>'content');
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'pic', 'title'=>'房源图片', 'name'=>'pics', 'num'=>2, 'pics'=>array());
        }elseif($info['sub_type'] == 'common'){
            if($info['pid'] != 0){
                $res['add_url'] = "https://www.58zltc.com/api.php/Info/add?type={$info['pid']}&sub_type={$info['id']}";
                $res['index_url'] = "/pages/index/common/index?id={$info['pid']}";
            }else{
                $res['add_url'] = "https://www.58zltc.com/api.php/Info/add?type={$info['id']}&sub_type=0";
                $res['index_url'] = "/pages/index/common/index?id={$info['id']}";
            }

            $form[] = array('type'=>'text', 'title'=>'标题', 'name'=>'title');
            $form[] = $site_input;
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'text', 'title'=>'联系人', 'name'=>'username');
            // $form[] = array('type'=>'text', 'title'=>'详细地址', 'name'=>'company_site');
            $form[] = array('type'=>'number', 'title'=>'联系电话', 'name'=>'tel');
            $form[] = array('type'=>'top');
            $form[] = $tags_input;
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'textarea', 'title'=>'信息详情', 'name'=>'content');
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'pic', 'title'=>'信息图片', 'name'=>'pics', 'num'=>2, 'pics'=>array());
        }elseif($info['sub_type'] == 'zhaoren'){
            $res['add_url'] = "https://www.58zltc.com/api.php/Pinche/add?type=1";
            $res['index_url'] = "/pages/index/pinche/index";

            $form[] = array('type'=>'text', 'title'=>'出发地', 'name'=>'start_site');
            $form[] = array('type'=>'text', 'title'=>'目的地', 'name'=>'end_site');
            $form[] = array('type'=>'date', 'title'=>'出发日期', 'name'=>'go_date', 'value'=>date("Y-m-d"));
            $form[] = array('type'=>'time', 'title'=>'出发时间', 'name'=>'go_time', 'value'=>date("H:i"));
            $form[] = array('type'=>'number', 'title'=>'座位数', 'name'=>'num');
            $form[] = array('type'=>'top');
            // $form[] = array('type'=>'text', 'title'=>'联系人', 'name'=>'username');
            // $form[] = array('type'=>'text', 'title'=>'详细地址', 'name'=>'company_site');
            $form[] = array('type'=>'number', 'title'=>'联系电话', 'name'=>'tel');
        }elseif($info['sub_type'] == 'zhaoche'){
            $res['add_url'] = "https://www.58zltc.com/api.php/Pinche/add?type=0";
            $res['index_url'] = "/pages/index/pinche/index";

            $form[] = array('type'=>'text', 'title'=>'出发地', 'name'=>'start_site');
            $form[] = array('type'=>'text', 'title'=>'目的地', 'name'=>'end_site');
            $form[] = array('type'=>'date', 'title'=>'出发日期', 'name'=>'go_date', 'value'=>date("Y-m-d"));
            $form[] = array('type'=>'time', 'title'=>'出发时间', 'name'=>'go_time', 'value'=>date("H:i"));
            $form[] = array('type'=>'number', 'title'=>'乘车人数', 'name'=>'num');
            $form[] = array('type'=>'top');
            // $form[] = array('type'=>'text', 'title'=>'联系人', 'name'=>'username');
            // $form[] = array('type'=>'text', 'title'=>'详细地址', 'name'=>'company_site');
            $form[] = array('type'=>'number', 'title'=>'联系电话', 'name'=>'tel');
        }

        $res['add_url'] .= "&cid={$info['pid']}&sub_cid={$info['id']}";
        $form[] = array('type'=>'top');
        $form[] = array('type'=>'price', 'title'=>'收费', 'price'=>$wechat['tongcheng_fee']);

        $eq['wid'] = session('wid');
        $top_arr = array("无需置顶");
        $top_arr2 = M("Top")->where($eq)->getField('title',true);
        if($top_arr2){
            $top_arr = array_merge($top_arr,$top_arr2);
        }
        $form[] = array('type'=>'picker1', 'title'=>'置顶', 'name'=>'top_fee' 
                ,'range'=>$top_arr
                , 'value'=>0);

        $res['form'] = $form;
        $this->ajaxReturn($res);
    }

    private function getSiteList(){

    }

    public function login(){
        $map['wid'] = session('wid');
        $info = M("Wechat")->where($map)->find();

        $component_appid = C("OPEN_APPID");
    	$appid = $info['appid'];
    	$appsrcret = $info['app_srcret'];
        $token = $this->get_component_access_token();

    	$url = "https://api.weixin.qq.com/sns/component/jscode2session?appid={$appid}&js_code={$_POST['code']}&grant_type=authorization_code&component_appid={$component_appid}&component_access_token={$token}";
    	$res = $this->ihttp_get($url);
    	$td = json_decode($res, true);
    	if($td['errmsg']){
            // dump($td);
  			$arr = array("err_code"=>400,"err_msg"=>$td['errmsg']);
    		$this->ajaxReturn($arr);
    	}
    	$map['openid'] = $td['openid'];
    	if(M("Member")->where($map)->find()){
    		$new['session_key'] = $td['session_key'];
    		M("Member")->where($map)->save($new);
    	}else{
    		$td['wid'] = session('wid');
    		$td['create_time'] = date("Y-m-d H:i:s");
    		M("Member")->add($td);
    	}
    	$arr = array("err_code"=>0, "openid"=>$td['openid']);
    	$this->ajaxReturn($arr);
    }

    // 设置头像、昵称
    public function user(){
        $map['wid'] = session('wid');
        $map['openid'] = I("post.openid");

        $data['nickname'] = I("post.nickName");
        $data['headimgurl'] = I("post.avatarUrl");
        M("Member")->where($map)->save($data);
    }

    private function getMemberInfo(){

    }

    public function cate(){
        $model = M("Cate");
    	$map = array();
        $map['wid'] = session('wid');
    	if(isset($_GET['id'])){
    		$map['pid'] = $_GET['id'];
    	}
        if($_GET['type']){
            $map['type'] = $_GET['type'];
        }
        if($_GET['need_sub'] == 1){
            $map['can_add'] = 1;
            $list = $model->where($map)->order("sort asc")->select();
            foreach ($list as $key => $value) {
                $eq['pid'] = $value['id'];
                $sub = $model->where($eq)->order("sort asc")->select();
                foreach ($sub as $k => $v) {
                    if($value['need_id']){
                        $sub[$k]['url'] = "{$v['add_url']}?id={$v['id']}";
                    }else{
                        $sub[$k]['url'] = $v['add_url'];
                    }
                }
                $list[$key]['url'] = $value['add_url'];
                $list[$key]['sub'] = $sub;
            }
        }else{
            $list = M("Cate")->where($map)->order("sort asc")->select();
            foreach ($list as $key => $value) {
                if($value['need_id']){
                    $list[$key]['url'] = "{$value['url']}?id={$value['id']}";
                }elseif($value['type'] == 'shop'){
                    $list[$key]['url'] = "list?id={$value['id']}";
                }
            }
        }
        $arr = array();
        $arr['title'] = '外卖';
        $arr['imgurl'] = 'https://www.58zltc.com/Uploads/2017-12-20/5a39cf2f2915f.png';
        $arr['url'] = 'takeout/home';
        array_push($list, $arr);
    	$this->ajaxReturn($list);
    }

    public function addFormId(){
        if($_POST['openid'] != 'undefined'){
            $data['openid'] = I("post.openid");
            $data['form_id'] = I("post.form_id");
            $data['max_num'] = $_POST['type'] == 'pay'?3:1;
            $data['used_num'] = 0;
            $data['end_time'] = date("Y-m-d H:i:s", strtotime("+7 days"));
            M("Formid")->add($data);
        }
    }

    public function upload(){
    	$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     3145728 ;// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg', 'mp4');// 设置附件上传类型
		$upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
		$upload->savePath  =      ''; // 设置附件上传（子）目录
		// 上传文件 
		$info   =   $upload->upload();
        // $str = "ffmpeg -i /www/web/zltc_qingyuntongcheng_com/public_html/Uploads/2017-12-14/5a323faca6d92.mp4  -y -f mjpeg -ss 3 -t 1 /www/web/zltc_qingyuntongcheng_com/public_html/Uploads/2017-12-14/test.jpg";
		if(!$info) {// 上传错误提示错误信息
			$arr = array("err_code"=>400,"err_msg"=>$upload->getError());
    		$this->ajaxReturn($arr);
		}else{// 上传成功 获取上传文件信息
			$file = $info['file'];
            $file_name = $file['savepath'].$file['savename'];
            $file_img_name = "";
            if(preg_match('/mp4/', $file['type'])){
                $file_img_name = str_replace('mp4', 'jpg', $file_name);
                shell_exec("ffmpeg -i /www/web/zltc_qingyuntongcheng_com/public_html/Uploads/{$file_name}  -y -f mjpeg -ss 3 -t 1 /www/web/zltc_qingyuntongcheng_com/public_html/Uploads/{$file_img_name}");
            }
			$arr = array("err_code"=>0,"file_name"=>$file_name, "file_img_name"=>$file_img_name);
    		$this->ajaxReturn($arr);
		}
    }


    //获取小程序的模板消息
    public function gettemplate()
    {
        $token = $this->get_access_token_mp();
        $post_data =array(
            'offset' => 0,
            'count' => 20
        );
        $url = "https://api.weixin.qq.com/cgi-bin/wxopen/template/list?access_token=".$token;
        $res = $this->ihttp_post($url,json_encode($post_data));
        $td = json_decode($res, true);
    }
    //发送模板消息测试
    public function sendtemplate()
    {
        $access_token = $this->get_access_token_mp();
        $data = $_POST;
        $touser = "oRVj50L-3jTgEqslDeC7sz8lc-Ds";
        $template_id = "GFnTfRRmlxitBmBZjhnUtP1gofufXI1iUGe88f-ByX4";
        $page = "pages/index/index";
        $form_id = I('formid');

        $keyword1 = "蓝海";//酒店名称
        $fee = "linyi.China";  //地点
        if(empty($keyword1)){
            exit('empty activity message!');
        }
        $value = array(
            "keyword1"=>array(
                "value"=>$keyword1,
                //"value"=>'woshihaoren',
                "color"=>"#4a4a4a"
            ),
            "keyword2"=>array(
                "value"=>'2018-04-25', //预订时间
                "color"=>"#9b9b9b"
            ),
            "keyword3"=>array(
                "value"=>"￥5.0",//金额
                "color"=>"#9b9b9b"
            ),
            "keyword4"=>array(
                "value"=>$fee,//地点
                "color"=>"#9b9b9b"
            ),
            "keyword5"=>array(
                "value"=>'2018-04-25',//入住时间
                "color"=>"#9b9b9b"
            ),
            "keyword6"=>array(
                "value"=>'2018-04-26',//离店时间
                "color"=>"#9b9b9b"
            ),
            "keyword7"=>array(
                "value"=>'大床房',//房间类型
                "color"=>"#9b9b9b"
            ),
            "keyword8"=>array(
                "value"=>'2人',//入住人
                "color"=>"#9b9b9b"
            ),
            "keyword9"=>array(
                "value"=>'20510',//房间数量
                "color"=>"red"
            )
        );

        $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$access_token;
        $dd = array();
        //$dd['access_token']=$access_token;
        $dd['touser']=$touser;
        $dd['template_id']=$template_id;
        $dd['page']=$page;  //点击模板卡片后的跳转页面，仅限本小程序内的页面。支持带参数,该字段不填则模板无跳转。
        $dd['form_id']=$form_id;

        $dd['data']=$value;                        //模板内容，不填则下发空模板

        $dd['color']='';                        //模板内容字体的颜色，不填默认黑色
        //$dd['color']='#ccc';
        $dd['emphasis_keyword']='';    //模板需要放大的关键词，不填则默认无放大
        //$dd['emphasis_keyword']='keyword1.DATA';

        //$send = json_encode($dd);   //二维数组转换成json对象

        /* curl_post()进行POST方式调用api： api.weixin.qq.com*/
        $result = $this->ihttp_post($url,json_encode($dd));
        $result = json_decode($result,true);
        if($result){
            echo json_encode(array('errcode'=>$result['errcode'],'msg'=>$result['errmsg']));
        }else{
            echo json_encode(array('errcode'=>$result['errcode'],'msg'=>$result['errmsg']));
        }
    }

    private function get_component_access_token(){
        $token = S("component_access_token");
        if($token){
            return $token;
        }
        $url = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";

        $post_data = array(
            "component_appid"=>C("OPEN_APPID"),
            "component_appsecret"=>C("OPEN_APPSECRET"),
            "component_verify_ticket"=>S("component_verify_ticket"));
        $json = json_encode($post_data);
        $res = $this->ihttp_post($url, $json);
        $td = json_decode($res, true);
        if($td['component_access_token']){
            S("component_access_token", $td['component_access_token'], $td['expires_in'] - 120);
            return $td['component_access_token'];
        }else{
            return false;
        }
    }

    //查数据获得分类检索
    public function getCate(){
        $model = M("Cate");
        $map = array();
        $map['wid'] = session('wid');
        if(isset($_GET['id'])){
            $map['pid'] = $_GET['id'];
        }
        if($_GET['type']){
            $map['type'] = $_GET['type'];
        }
        if($_GET['need_sub'] == 1){
            $map['can_add'] = 1;
            $list = $model->where($map)->order("sort asc")->select();
            foreach ($list as $key => $value) {
                $eq['pid'] = $value['id'];
                $sub = $model->where($eq)->order("sort asc")->select();
                foreach ($sub as $k => $v) {
                    if($value['need_id']){
                        $sub[$k]['url'] = "{$v['add_url']}?id={$v['id']}";
                    }else{
                        $sub[$k]['url'] = $v['add_url'];
                    }
                }
                $list[$key]['url'] = $value['add_url'];
                $list[$key]['sub'] = $sub;
            }
        }else{
            $list = M("Cate")->where($map)->order("sort asc")->select();
            foreach ($list as $key => $value) {
                if($value['need_id']){
                    $list[$key]['url'] = "{$value['url']}?id={$value['id']}";
                }elseif($value['type'] == 'shop'){
                    $list[$key]['url'] = "list?id={$value['id']}";
                }
            }
        }
        //处理list返回需要数组
        $arr['title'] = '全部分类';
        $arr['is_show'] = false;
        $arr['list'] = array();
        $arr['list'][0] = array('id'=> 0,'value'=>'全部分类', 'is_selected'=>true);
        foreach($list as $k=>$v) {
            $arr['list'][] = array(
                'id' =>         $v['id'],
                'value' =>      $v['title'],
                'is_selected'=> false
            );
        }
        return $this->ajaxReturn($arr);
    }

    //保存商户广告信息
    public function uploadShopImg () {
        $request = file_get_contents('php://input');
        $arr = json_decode($request,true);
        $map = array();
        $shop = M('shop')->where(['shop_id' => $arr['shop_id']])->find();
        if(!$shop){
            $msg = 0;
            return $this->ajaxReturn($msg);
        }
        //$map['shop_link'] = $arr['link'];
        //$map['shop_advimg'] = $arr['file_name'];
        $shop_banner = implode(",",$arr['file_name2']);//店铺轮播
        $pics = implode(",",$arr['file_name']);//店铺相册
        if($arr['link']){
            $map['shop_video'] = $arr['link'];//店铺视频
        }

        $map['shop_banner'] = $shop_banner ? $shop_banner : $shop['shop_banner'];
        $map['pics'] = $pics ? $pics : $shop['pics'];

        $row = M('shop')->where(['shop_id' => $arr['shop_id']])->save($map);
        $row ? $msg = 1 : $msg = 0;
        return $this->ajaxReturn($msg);
    }
    //获取单个商户信息
    public function getShopInfo () {
        $info = M('shop')->where(['shop_id' => $_GET['id']])->find();
        if($info['shop_banner']){
            $info['shop_banner'] = explode(",",$info['shop_banner']);
        }
        if($info['pics']){
            $info['pics'] = explode(",",$info['pics']);
        }
        return $this->ajaxReturn($info);
    }

    //
    public function getAdvShop () {
        $where['wid'] = session('wid');
        $where['status'] = 1;
        $where['type'] = 1;
        $arr = M("adv_shop")->where($where)->find();
        if ($arr) {
            $shop_id = $arr['shop_id'];
            $shop = M('shop')->field('shop_link, shop_advimg')->where(['shop_id' => $shop_id])->find();
            $this->ajaxReturn($shop);
        }
        $this->ajaxReturn($arr);
    }
    //获取资讯分类列表
    public function cateList(){
        $map = array();
        $map['status'] = 1;
        $map['wid'] = session('wid');
        $list = M("index_cate")->where($map)->order('add_time desc')->select();
        $this->ajaxReturn($list);
    }

    //
    public function savePersonalInfo () {
        $request = file_get_contents('php://input');
        $arr = json_decode($request,true);

        //插入数据
        $row = M('member')->where(['openid' => $_GET['openid'], 'wid' => session('wid')])->save($arr);

        $row ? $this->ajaxReturn(1) : $this->ajaxReturn(0);
    }

    //
    public function getPersonalInfo () {
        $arr = M('member')->where(['openid' => $_GET['openid'], 'wid' => session('wid')])->select();
        $this->ajaxReturn($arr);
    }
}